package com.china08.yxyapi.api.manage.tec.teaching.homework.myques;

import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.homework.HomeworkQuesGroup;
import com.china08.yxyapi.entity.mg.homework.HomeworkQuesGroupRelQues;
import com.china08.yxyapi.entity.mg.res.YxyResQues;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyResQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkQuesGroupRelQuesRepository;
import com.china08.yxyapi.repository.mg.homework.HomeworkQuesGroupRepository;
import com.china08.yxyapi.util.EsQuestionUpdateUtil;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class HomeworkGroupDelController extends YxyApiBaseController {

    @Autowired
    private HomeworkQuesGroupRepository homeworkQuesGroupRepository;

    @Autowired
    private HomeworkQuesGroupRelQuesRepository homeworkQuesGroupRelQuesRepository;

    @Autowired
    private YxyResQuesRepository yxyResQuesRepository;

    @ApiOperation(value = "删除组")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/homework/mysql/group/del/{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public YxyApiRespModel process(HttpServletRequest request, HttpServletResponse response,
            @PathVariable(value = "id") String id) {
        String teacherId = getProfile(request, response).getId();
        HomeworkQuesGroup homeworkQuesGroup = homeworkQuesGroupRepository.findById(id);
        Assert.notNull(homeworkQuesGroup, "组不存在");
        Assert.isTrue(StringUtils.equals(homeworkQuesGroup.getTeacherId(), teacherId), "这不是您的分组");

        Assert.isTrue(!StringUtils.equals("myques", id), "基本分组不可删除");
        // 删除组关联
        List<HomeworkQuesGroupRelQues> groupRelQues = homeworkQuesGroupRelQuesRepository.findByGroupId(id);

        List<String> quesIds = groupRelQues.stream().map(x -> x.getQuesId()).collect(Collectors.toList());
        if (quesIds.size() > 0) {
            List<YxyResQues> yxyResQues = yxyResQuesRepository.findByIdIn(quesIds);
            for (YxyResQues yrq : yxyResQues) {
                if (StringUtils.equals(teacherId, yrq.getCreatedBy())) {
                    yrq.setSycEsFlag(0);
                    yrq.setDeleteFlag("1");
                    // 删除大题库 试题（改状态） 只能改是自己的
                    yxyResQuesRepository.save(yrq);
                    // 通知es 自己的题库发生改变
                    EsQuestionUpdateUtil.updateEsQuestion(yrq.getId());
                }
            }
            // 删除组和试题的关联
            homeworkQuesGroupRelQuesRepository.delete(groupRelQues);
        }
        // 删除组
        homeworkQuesGroupRepository.delete(homeworkQuesGroup);
        return new YxyApiRespModel("删除成功");
    }

}
